import pinia, { useUserStore } from '@/store';
import { getUserOpenid, OpenIdItem, getSilenterInfo } from '@/service/user';

const checkSession = () => {
	return new Promise((resolve, reject) => {
		uni.checkSession({
			success() {
				resolve(true);
			},
			fail() {
				reject(false);
			},
		});
	});
};

const getLoginCode = () => {
	return new Promise((resolve, reject) => {
		uni.login({
			provider: 'weixin',
			success(res) {
				if (res.code) {
					resolve(res);
				} else {
					reject(res);
				}
			},
			fail(err) {
				reject(err);
			},
		});
	});
};

const checkLogin = () => {
	const userStore = useUserStore(pinia);
	return new Promise((resolve, reject) => {
		if (userStore.userInfo.session_key) {
			checkSession()
				.then(() => {
					resolve({
						openid: userStore.userInfo.openid,
						session_key: userStore.userInfo.session_key,
						unionid: userStore.userInfo.unionid,
					});
				})
				.catch(() => {
					reject(false);
				});
		} else {
			reject(false);
		}
	});
};

const getOpenId = (showLoading = false) => {
	return new Promise((resolve, reject) => {
		checkLogin()
			.then(loginInfo => {
				resolve(loginInfo);
			})
			.catch(() => {
				getLoginCode()
					.then(async (res: { code: string }) => {
						getSessionKey(res.code, resolve, reject, showLoading);
					})
					.catch(() => {
						if (showLoading) {
							uni.showToast({
								icon: 'none',
								title: '登录失败~',
							});
						}
						reject();
					});
			});
	});
};

const getSessionKey = async (code: string, resolve, reject, showLoading = false) => {
	const userStore = useUserStore(pinia);
	try {
		const data = await getUserOpenid(code);
		if (data.status_code === 200) {
			userStore.setTokenInfo({ ...userStore.tokenInfo, ...data.data });
			resolve(data.data as OpenIdItem);
		} else {
			if (showLoading) {
				uni.showToast({
					icon: 'none',
					title: data.message || '登录失败~',
				});
			}
			reject();
		}
	} catch (error) {
		if (showLoading) {
			uni.showToast({
				icon: 'none',
				title: '登录失败~',
			});
		}
		reject();
	}
};

const getLoginSilent = async () => {
	const userStore = useUserStore(pinia);
	return new Promise((resolve, reject) => {
		getLoginCode()
			.then(async (res: { code: string }) => {
				try {
					const login = await getSilenterInfo(res.code);
					if (login.status_code === 200 && login.data) {
						const default_student = login.data.default_student || {};
						userStore.setUserInfo({
							...default_student,
						});
						userStore.setTokenInfo({
							...userStore.tokenInfo,
							token: login.data.token.token_type + login.data.token.access_token,
						});
						resolve(true);
					} else {
						reject(false);
					}
				} catch (error) {
					reject(false);
				}
			})
			.catch(() => {
				reject(false);
			});
	});
};

export default {
	checkSession,
	getLoginCode,
	checkLogin,
	getOpenId,
	getLoginSilent,
};
